package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.ManagerDaoImpl;
import com.wanxi.dao.ManagerDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Manager;
import com.wanxi.service.ManagerService;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.Md5;
import com.wanxi.vo.ManagerVO;

import java.util.List;

public class ManagerServiceImpl implements ManagerService {
    ManagerDao managerDao = new ManagerDaoImpl();
    @Override
    public ResultDTO login(ManagerVO managerVO) {
        if(managerVO.getCode()!=null&&managerVO.getCode().equals(managerVO.getVCode())){
            Manager manager = Manager.builder()
                    .managerName(managerVO.getManagerName())
                    .password(Md5.encoderByMd5(managerVO.getPassword()))
                    .build();
            Manager m = managerDao.findManagerByManagerNameAndPassword(manager);
            if (m.getEnable() != null && m.getEnable() == 1) {
                return ResultDTO.builder().code(0).msg("success").data(m).count(1).build();
            } else {
                return ResultDTO.builder().code(1).msg("error").data(managerVO).count(0).build();
            }
        }else
            return ResultDTO.builder().msg("验证码错误").code(-1).build();
    }

    @Override
    public ResultDTO findAllManager() {
        List<Manager> list =managerDao.findAllManager();
//        保证密码不泄露
        if (list.size() > 0) {
            list.forEach(manager -> {
                manager.setPassword("******");
            });
           return ResultDTO.getDTO(list.size(),list);
        } else {
            return ResultDTO.builder().count(0).data(list).msg("no data").build();
        }
    }

    @Override
    public ResultDTO findManagerById(ManagerVO managerVO) {
        Manager manager = Manager.builder()
                .id(managerVO.getId())
                .build();
        Manager m = managerDao.findManagerById(manager);
        if (m != null) {
            return ResultDTO.getDTO(1,m);
        } else {
            return ResultDTO.builder().count(0).msg("error").code(1).build();
        }
    }

    @Override
    public ResultDTO addManager(ManagerVO managerVO) {
        Manager manager = Manager.builder()
                .managerName(managerVO.getManagerName().toLowerCase())
                .password(Md5.encoderByMd5(managerVO.getPassword()))
                .remark(managerVO.getRemark())
                .build();
        if (managerVO.getEnable() == null)
            manager.setEnable(0);
        Manager m = managerDao.findManagerByName(manager);
        if (m == null) {
            int i = managerDao.addManager(manager);
            if (i == 1) {
                return ResultDTO.getDTO(i,managerVO);
            }
        }
        return ResultDTO.builder().code(1).count(0).msg("error").data(managerVO).build();
    }

    @Override
    public ResultDTO updatePassword(ManagerVO managerVO) {
        if (managerVO.getOldPassword() != null && !managerVO.getOldPassword().equals("")) {
            Manager manager = Manager.builder()
                    .id(managerVO.getId())
                    .oldPassword(Md5.encoderByMd5(managerVO.getOldPassword()))
                    .newPassword(Md5.encoderByMd5(managerVO.getNewPassword())).build();
            Manager m = managerDao.findManagerByIdAndPssword(manager);
            if (m != null) {
                int i = managerDao.updatePassword(manager);
                if (i == 1)
                    return ResultDTO.getDTO(i,managerVO);
            }
            return ResultDTO.builder().msg("原密码错误").count(0).code(1).data(managerVO).build();
        }
        return ResultDTO.builder().msg("error").count(0).code(1).data(managerVO).build();
    }

    @Override
    public ResultDTO deleteManager(ManagerVO managerVO) {
        Manager manager = Manager.builder().id(managerVO.getId()).build();
        int i = managerDao.deleteManager(manager);
        if (i == 1) {
            return ResultDTO.getDTO(i,managerVO);
        }
        return ResultDTO.builder().count(0).data(managerVO).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateById(ManagerVO managerVO) {
        Manager manager = new Manager();
        manager.setId(managerVO.getId());
        manager.setManagerName(managerVO.getManagerName());
        manager.setRemark(managerVO.getRemark());
        if(managerVO.getId()!=null){
            Manager m = managerDao.findManagerById(manager);
//            密码处理
            if (managerVO.getPassword() != null && managerVO.getPassword().equals("1")) {
                manager.setPassword(Md5.encoderByMd5(null));
            } else if (managerVO.getPassword() == null || managerVO.getPassword().equals("0")) {
                manager.setPassword(m.getPassword());
            }
//            状态处理
            if (managerVO.getEnable() == null)
                manager.setEnable(m.getEnable());
            else
                manager.setEnable(managerVO.getEnable());
//            用户名处理
            if(managerVO.getManagerName()==null)
                manager.setManagerName(m.getManagerName());

            int i = managerDao.updateById(manager);
            if (i == 1) {
                return ResultDTO.getDTO(i,managerVO);
            }
        }
        return ResultDTO.builder().msg("error").code(1).count(0).data(managerVO).build();
    }

    @Override
    public ResultDTO updateEnable(ManagerVO managerVO) {
        Manager manager= Manager.builder().id(managerVO.getId()).enable(managerVO.getEnable()).build();
        if(managerVO.getEnable()==null){
            manager.setEnable(0);
        }
        int i=managerDao.updateEnable(manager);
        if (i == 1) {
            return ResultDTO.getDTO(i,managerVO);
        }
        return ResultDTO.builder().data(managerVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findManagerBySome(ManagerVO managerVO) {
        Manager manager=JSONCopy.copy(managerVO,Manager.class);
        List<Manager> list =managerDao.findSomeManager(manager);
//        保证密码不泄露
        if (list.size() > 0) {
            list.forEach(m -> {
                m.setPassword("******");
            });
            return ResultDTO.getDTO(list.size(),list);
        } else {
            return ResultDTO.builder().count(0).data(list).msg("no data").build();
        }
    }
}
